import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { FileInfo } from "./types";
import { FileTableForm } from "../folder/types";

/**
 * 上传文件
 *
 * @param file
 */
export function uploadFileApi(file: File): AxiosPromise<FileInfo> {
  const formData = new FormData();
  formData.append("file", file);
  return request({
    url: "/api/v1/files",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}

/**
 * 删除文件
 *
 * @param filePath 文件完整路径
 */
export function deleteFileApi(filePath?: string) {
  return request({
    url: "/api/v1/files",
    method: "delete",
    params: { filePath: filePath },
  });
}

/**
 * 获取头像
 *
 * @param filePath 文件完整路径
 */
export function avatar(): AxiosPromise<string> {
  return request({
    url: "/api/v1/file/avatar",
    method: "get",
  });
}

/**
 * 上传头像
 *
 * @param file
 * @param md5
 * @returns
 */
export function uploadAvatar(file: File, md5: string) {
  return request({
    url: "/api/v1/file/uploadAvatar",
    method: "post",
    headers: {
      "Content-Type": "multipart/form-data",
    },
    data: {
      file,
    },
    params: { md5 },
  });
}

/**
 * 上传文件
 *
 * @param file
 * @param md5
 * @returns
 */
export function uploadExpAnnex(file: File, md5: string, courseId: number) {
  const formData = new FormData();
  formData.append("file", file);
  return request({
    url: "/api/v1/file/uploadExpAnnex",
    method: "post",
    headers: {
      "Content-Type": "multipart/form-data",
    },
    data: formData,
    params: { md5, courseId },
  });
}

/**
 * 上传课程头像
 *
 * @param file
 * @param md5
 * @returns
 */
export function uploadWallpaperUrl(file: File, md5: string, courseId: number) {
  return request({
    url: "/api/v1/file/uploadWallpaperUrl",
    method: "post",
    headers: {
      "Content-Type": "multipart/form-data",
    },
    data: {
      file,
    },
    params: { md5, courseId },
  });
}

/**
 * 获取课程头像
 *
 * @param filePath 文件完整路径
 */
export function wallpaperUrl(courseId: number): AxiosPromise<string> {
  return request({
    url: "/api/v1/file/wallpaperUrl",
    method: "get",
    params: {
      courseId,
    },
  });
}

/**
 *
 * @param filePath 文件完整路径
 */
export function previer(filePath: string) {
  return request({
    url: "/api/v1/file/preview",
    method: "get",
    params: {
      filePath,
    },
  });
}

/**
 *
 * @param filePath 文件完整路径
 */
export function downloadFile(filePath: string, fileName: string) {
  return request({
    url: "/api/v1/file/download",
    method: "get",
    params: {
      filePath,
      fileName,
    },
    responseType: "blob",
  });
}

/**
 *
 * @param filePath 文件完整路径
 */
export function downloadFiles(data: FileTableForm[]) {
  return request({
    url: "/api/v1/file/downloadResource",
    method: "post",
    data,
    responseType: "blob",
  });
}
